﻿//========================================================================
// *作者：海盗猫  主页：ilplay.com
// *脚本：EditorInspectorError
// *描述：EditorInspectorError
// *?2024 HDMCode . All rights reserved.
//* ======================================================================
using HDMCode.FrameEditor;
using HDMCode.UIFrame;
using UnityEditor;
using UnityEngine;

namespace HDMCode.UIFrameEditor
{
    public class EditorInspectorError
    {
        /// <summary>
        /// Editor重构脚本
        /// </summary>
        private UIDisplay m_Script;
        
        /// <summary>
        /// 初始化构造并将初始化值给基类
        /// </summary>
        /// <param name="script">初始化脚本</param>
        public EditorInspectorError(UIDisplay script) => m_Script = script;
        
        /// <summary>
        /// 绘制资源异常或者丢失下的Inspector页面
        /// </summary>
        public void DrawError()
        {
            UnityEditor.EditorGUILayout.HelpBox(@"
预制体对应环境丢失，请通过以下方式检查资源环境！
1.不支持直接挂载该组件，需通过菜单创建预制体。
2.检查当前预制体路径是否处于正确路径下。
3.检查当前预制体名称是否经过修改，若经过修改，需通过检查窗口手动修复环境。
", MessageType.Error);
            HDMEditorHelper.DrawButton("前往检查", "ErrorIcon.png", new[] { GUILayout.Height(30), GUILayout.ExpandWidth(true) }, () =>
            {
                EditorSearchPage window = (EditorSearchPage)EditorWindow.GetWindow(typeof(EditorSearchPage),true,"海盗猫UI预览窗口");
                window.Show();
            });
        }

        /// <summary>
        /// 资源检查
        /// </summary>
        public bool CheckAssets()
        {
            foreach (var item in EditorBaseDate.Setting.UISettings)
            {
                if (string.Equals(item.CreatName,m_Script.gameObject.name))
                {
                    m_Script.uISingleName = item.CreatName;
                    m_Script.scriptDescribe = item.Describe;
                    return true;
                }
            }
            return false;
        }
        
        /// <summary>
        /// 绘制资源名称
        /// </summary>
        public void DrawAssetsName()
        {
            using (new UnityEditor.EditorGUILayout.HorizontalScope()) 
            {
                EditorGUI.BeginDisabledGroup(true);
                Texture tileIcon = EditorGUIUtility.IconContent("cs Script Icon").image;
                GUILayout.Label(tileIcon, GUILayout.Width(20), GUILayout.Height(18));
                string path = AssetDatabase.GetAssetPath(m_Script.gameObject);
                string guid = AssetDatabase.AssetPathToGUID(path);
                if (EditorHelper.ContainsSettingByGuid(guid,out var value))
                {
                    m_Script.uISingleName = UnityEditor.EditorGUILayout.TextField("历史编译名称：", value.CreatName , GUILayout.Height(20));
                    EditorGUI.EndDisabledGroup(); 
                }
            }
        }
        
        /// <summary>
        /// 绘制资源描述
        /// </summary>
        public void DrawAssetsDescription()
        {
            using (new UnityEditor.EditorGUILayout.HorizontalScope()) 
            {
                EditorGUI.BeginDisabledGroup(true);
                Texture tileIcon = EditorGUIUtility.IconContent("Clipboard").image;
                GUILayout.Label(tileIcon, GUILayout.Width(20), GUILayout.Height(18));
                string path = AssetDatabase.GetAssetPath(m_Script.gameObject);
                string guid = AssetDatabase.AssetPathToGUID(path);
                if (EditorHelper.ContainsSettingByGuid(guid,out var value))
                    m_Script.scriptDescribe = UnityEditor.EditorGUILayout.TextField("资源描述：", value.Describe , GUILayout.Height(20));
                EditorGUI.EndDisabledGroup();
            }
            UnityEditor.EditorGUILayout.Space();
        }

    }
}
